package com.web.imp;

import java.lang.reflect.Field;

import com.app.AnnFieldPopulator;
import com.web.annotations.PageElement;
import com.web.model.WebElement;
import com.web.model.element.ElementFactory;

public class ElementPopulator extends AnnFieldPopulator<WebElement, PageElement>{

	private ElementFactory elementFactory;
	
	public ElementPopulator(ElementFactory elementFactory) {
		super(PageElement.class);
		this.elementFactory = elementFactory;
	}

	@Override
	protected WebElement newFieldValue(Field field, PageElement annotation) {
		
		String id = annotation.id();
		
		@SuppressWarnings("unchecked")
		Class<? extends WebElement> elementClass = (Class<? extends WebElement>) field.getType();
		
		WebElement element = elementFactory.getElement(elementClass, id);
		
		return element;
	}
}
